﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;

namespace DrawingScreen.Objects
{
    public class CRectangle: CDrawingObject
    {
        public int x1;
        public int y1;
        public int x2;
        public int y2;

        public int x;
        public int y;
        public int width;
        public int height;

        public Pen pen = new Pen(Color.Black);

        public void InitOtherParameters()
        {
            if (x1 < x2)
            {
                x = x1;
            }
            else {
                x = x2;
            }

            if (y1 < y2)
            {
                y = y1;
            }
            else
            {
                y = y2;
            }

            width = System.Math.Abs(x1 - x2);
            height = System.Math.Abs(y1 - y2);
        }

        public override void Draw(System.Drawing.Graphics g)
        {
            g.DrawRectangle(pen, x, y, width, height);
        }

        public override XmlNode convertToXmlNode(XmlDocument doc)
        {
            try
            {
                XmlElement eleObject = doc.CreateElement("OBJECT");

                XmlAttribute attObjectID = doc.CreateAttribute("id");
                attObjectID.Value = CDrawingObject.LINE_ID.ToString();
                eleObject.Attributes.Append(attObjectID);

                //////////////////////////////////////////////////////////////
                XmlElement eleRectangle = doc.CreateElement("RECTANGLE");

                XmlAttribute attX = doc.CreateAttribute("x");
                attX.Value = x.ToString();
                eleRectangle.Attributes.Append(attX);

                XmlAttribute attY = doc.CreateAttribute("y");
                attY.Value = y.ToString();
                eleRectangle.Attributes.Append(attY);

                XmlAttribute attW = doc.CreateAttribute("w");
                attW.Value = width.ToString();
                eleRectangle.Attributes.Append(attW);

                XmlAttribute attH = doc.CreateAttribute("h");
                attH.Value = height.ToString();
                eleRectangle.Attributes.Append(attH);

                XmlElement elePen = Global.CreateXmlElementFromPen(doc, pen);
                eleRectangle.AppendChild(elePen);

                //////////////////////////////////////////////////////////////

                eleObject.AppendChild(eleRectangle);

                return eleObject;
            }
            catch
            {
                return null;
            }
        }

        public static CRectangle CreateDrawingObject(XmlElement eleObject)
        {
            try
            {
                XmlElement eleLRectangle = (XmlElement)eleObject.FirstChild;

                CRectangle rect = new CRectangle();
                rect.x = Int32.Parse(eleLRectangle.Attributes["x"].Value);
                rect.y = Int32.Parse(eleLRectangle.Attributes["y"].Value);
                rect.width = Int32.Parse(eleLRectangle.Attributes["w"].Value);
                rect.height = Int32.Parse(eleLRectangle.Attributes["h"].Value);

                Pen pen = Global.CreatePenFromXmlElement((XmlElement) eleLRectangle.FirstChild);

                if (pen != null)
                {
                    rect.pen = pen;
                }

                return rect;

            }
            catch
            {
                return null;
            }
        }
    }
}
